package com.demo;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.alibaba.fastjson.JSON;

public class StreamTest {
	
	public static void main(String args[]) {
		Stream<String> s = Stream.of("test", "t1", "t2", "teeeee", "aaaa", "taaa");
		String s1 = s.reduce( "|" , (a , b) -> {
			return a + b; 
		}) ;
		System.out.println(s1);
		Stream<String> ss = Stream.of("test", "t1", "t2", "teeeee", "aaaa", "taaa");
		
		Optional<String> s2 = ss.reduce(  (a , b) -> {
			System.out.println(a);
			return a + "|" +  b; 
		}) ;
		System.out.println(s2.get());
		
         Stream<String> sss = Stream.of("test", "t1", "t2", "teeeee", "aaaa", "taaa");
		
		String s3 = sss.reduce( "KEY" , (k , v) -> {
			return k + '&' + v ;
		} ,  (a , b) -> {
			System.out.println(b);
			return a + "|" +  b; 
		}) ;
		System.out.println(s3);
		
		
		  Stream<String> ssss = Stream.of("test", "t1", "t2", "teeeee", "aaaa", "taaa");
		  
		  Map<Integer,List<String>> j1 = ssss.collect(Collectors.groupingBy(String::length , Collectors.toList())) ;
		  
		  System.out.println(JSON.toJSONString(j1));
			
		/*//1.分组计数
	        List<Student> list1= Arrays.asList(
	                new Student(1,"one","zhao"),new Student(2,"one","qian"),new Student(3,"two","sun"));
	        //1.1根据某个属性分组计数
	        Map<String,Long> result1=list1.stream().collect(Collectors.groupingBy(Student::getGroupId,Collectors.counting()));
	        System.out.println(result1);
	        //1.2根据整个实体对象分组计数,当其为String时常使用
	        Map<Student,Long> result2=list1.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
	        System.out.println(result2);
	        //1.3根据分组的key值对结果进行排序、放进另一个map中并输出
	        Map<String,Long> xMap=new HashMap<>();
	        result1.entrySet().stream().sorted(Map.Entry.<String,Long>comparingByKey().reversed()) //reversed不生效
	                .forEachOrdered(x->xMap.put(x.getKey(),x.getValue()));
	        System.out.println(xMap);

	        //2.分组，并统计其中一个属性值得sum或者avg:id总和
	        Map<String,Integer> result3=list1.stream().collect(
	                Collectors.groupingBy(Student::getGroupId,Collectors.summingInt(Student::getId))
	        );
	        System.out.println(result3);
*/
	}
}
